package com.study;

import com.study.tool.StringTool;

public class PrintNum4Ielts02 {

    public static void main(String[] args) {
        int loopBig = 6;
        int loop = 8;
        String alert = "No #No ready, 3, 2, 1 begin:";
        int decimalRange = 5;
        int intergerlengthRangeStart = 5;
        int intergerlengthRangeEnd = 12;

        for(int k=1;k<=loopBig;k++) {
            System.out.println();
            System.out.println();
            System.out.println();
            String tmp = alert.replace("#No", String.valueOf(k));
            System.out.println(tmp);
            System.out.println();
            for (int i = 1; i <= loop; i++) {
                int decimalNum = StringTool.getRandomInRange(0, decimalRange);
                int zeroNum = StringTool.getRandomInRange(5, 10);
                int intergerlength = StringTool.getRandomInRange(intergerlengthRangeStart, intergerlengthRangeEnd);
                String num = getNum4Ielts2(intergerlength, decimalNum);
                // System.out.println(i+": "+num);
                System.out.println("The price is $" + num);
            }
        }
    }

    private static String getDecimal(int decimalNum) {
        StringBuffer buffer = new StringBuffer();
        if (decimalNum >0){
            String designedLengthRandom = StringTool.getDesignedLengthRandom(decimalNum);
            buffer.append(".");
            buffer.append(designedLengthRandom);
        }
        return buffer.toString();
    }

    public static String getNum4Ielts2(int integerlengh, int decimalNum){
        String str = StringTool.getDesignedLengthRandom(integerlengh);

        while (str.startsWith("0")){
            str = str.substring(1);
        }

        // locate zero index
        int length = str.length();
        int zeroIndex = StringTool.getRandomInRange(1, length-1);

        // split into prefix & suffix
        String prefix = str.substring(0, zeroIndex);
        String suffix = str.substring(zeroIndex);

        int random = StringTool.getRandomInRange(0, 9);
        // decide how many zero
        if (random%2!=0){
            int maxZeroNum = suffix.length();
            int zeroNum = StringTool.getRandomInRange(1, maxZeroNum);

            suffix = suffix.substring(zeroNum);
            for (int j=0;j<zeroNum;j++) {
                suffix = "0"+suffix;
            }
        }

        String integerFull = prefix+suffix;
        //String commaFull = integerFull;//comma(integerFull);
        String commaFull = comma(integerFull);
        while (integerFull.startsWith("0")){
            integerFull = integerFull.substring(1);
        }
        String decimal = getDecimal(decimalNum);
        String full = commaFull+decimal;

        while (full.startsWith("0")){
            full = full.substring(1);
        }
        return full;
    }

    public static String comma(String prefix){
        int tmp = prefix.length()%3;

        int zeroPrefixNum = 0;
        if (tmp != 0) {
            zeroPrefixNum = 3 - tmp;
        }

        for (int i = 0; i < zeroPrefixNum; i++) {
            prefix = "0"+prefix;
        }

        StringBuilder builder = new StringBuilder();
        int time = prefix.length()/3;
        for (int i = 0; i < time; i++) {
            if (prefix.length()==3){
                builder.append(prefix);
                break;
            }
            String tmp2 = prefix.substring(0, 3);
            builder.append(tmp2);
            builder.append(",");
            prefix = prefix.substring(3);
        }
        return builder.toString();
    }

}
